package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo6 {
    public static void main(String[] args) {
        /*try(Connection connection = DBUtil.getConnection();) {
            Statement statement = connection.createStatement();
            String sql = "SELECT t.name,t.salary,c.name,c.floor\n" +
                    "    FROM teacher t\n" +
                    "JOIN class c ON t.id = c.teacher_id\n" +
                    "WHERE t.name = '王克晶' ";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                String tname = rs.getString("t.name");
                int tsalary = rs.getInt("t.salary");
                String cname = rs.getString("c.name");
                int cfloor = rs.getInt("c.floor");
                System.out.println(tname+","+tsalary+","+cname+","+cfloor);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }*/


        try( Connection connection = DBUtil.getConnection();) {
            Statement statement = connection.createStatement();
            String sql = "SELECT t.name,t.salary,c.name,c.floor\n" +
                    "    FROM teacher t\n" +
                    "JOIN class c ON t.id = c.teacher_id\n" +
                    "WHERE t.name = '王克晶' ";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                String tName = rs.getString("t.name");
                int tSalary = rs.getInt("t.salary");
                String cName = rs.getString("c.name");
                int cFloor = rs.getInt("c.floor");
//                System.out.println(tName+","+tSalary+","+cName+","+cFloor);
                //String.join()方法的第一位元素是分隔符，表现在几个元素用什么符号间隔
                String line = String.join("--",tName,tSalary+"",cName,cFloor+"");
                System.out.println(line);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }
}
